package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.News;
import com.ruoyi.system.service.INewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.regex.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 资讯Controller
 *
 * @author gzhuangwei
 * @date 2022-02-26
 */
@Api("新闻信息管理")
@RestController
@RequestMapping("/business/news")
public class NewsController extends BaseController
{
    @Autowired
    private INewsService newsService;

    /**
     * 查询资讯列表
     */
    @PreAuthorize("@ss.hasPermi('business:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(News news)
    {
        startPage();
        // 得到帐户用户名和用户类型
        List<News> list = newsService.selectNewsList(news);
        return getDataTable(list);
    }

    /**
     * 查询新闻列表
     */
    @ApiOperation("获取在官网展示的新闻列表")
    @GetMapping("/listall")
    public AjaxResult newsListAll(News news)
    {
        news.setState("approval");
        startPage();
        List<News> list = newsService.selectNewsList(news);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 获取资讯新闻详细信息
     */
    @ApiOperation("获取资讯新闻详细信息")
    @GetMapping(value = "/detail/{newsId}")
    public AjaxResult getDetail(@PathVariable("newsId") Long newsId)
    {
        News news = newsService.selectNewsById(newsId);
        String newsIntro = news.getIntro();
        newsIntro = newsIntro.replaceAll("/prod-api","http://i.smartpants.cn/prod-api");
        newsIntro = newsIntro.replaceAll("/dev-api","http://i.smartpants.cn/prod-api");
        newsIntro = newsIntro.replaceAll("class=\"ql-align-center\"","style=\"text-align:center\"");
        newsIntro = newsIntro.replaceAll("class=\"ql-align-right\"","style=\"text-align:right\"");
        newsIntro = newsIntro.replaceAll("class=\"ql-align-left\"","style=\"text-align:left\"");
        news.setIntro(newsIntro);
        return AjaxResult.success(news);
    }

    /**
     * 导出资讯列表
     */
    @PreAuthorize("@ss.hasPermi('business:news:export')")
    @Log(title = "资讯", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(News news)
    {
        List<News> list = newsService.selectNewsList(news);
        ExcelUtil<News> util = new ExcelUtil<News>(News.class);
        return util.exportExcel(list, "资讯数据");
    }

    /**
     * 获取资讯详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId)
    {
        return AjaxResult.success(newsService.selectNewsById(newsId));
    }

    /**
     * 新增资讯
     */
    @PreAuthorize("@ss.hasPermi('business:news:add')")
    @Log(title = "资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody News news)
    {
        // 创建日期Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        // 得到创建者帐户ID
        Long createId = SecurityUtils.getLoginUser().getUser().getUserId();
        // 得到创建人帐户用户名
        String createBy = SecurityUtils.getLoginUser().getUser().getUserName();
        // 新增流向来源，设置审核状态为：待提交
        news.setState("initial");
        news.setCreateid(createId);
        news.setCreatetime(timeStamp);
        news.setCreateby(createBy);
        return toAjax(newsService.insertNews(news));
    }

    /**
     * 修改资讯
     */
    @PreAuthorize("@ss.hasPermi('business:news:edit')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody News news)
    {
        // 修改日期Date对象
        Date date = new Date();
        // 得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Timestamp timeStamp = new Timestamp(date.getTime());
        // 得到修改者帐户ID
        Long updateId = SecurityUtils.getLoginUser().getUser().getUserId();
        // 得到修改人帐户ID
        String updateBy = SecurityUtils.getLoginUser().getUser().getUserName();
        news.setUpdateid(updateId);
        news.setUpdatetime(timeStamp);
        news.setUpdateby(updateBy);
        return toAjax(newsService.updateNews(news));
    }

    /**
     * 提交资讯供审批
     */
    @PreAuthorize("@ss.hasPermi('business:news:submit')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @DeleteMapping("/submit/{newsIds}")
    public AjaxResult submit(@PathVariable Long[] newsIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        News news = new News();
        news.setState("approval");
        for(Long newsId : newsIds){
            news.setId(newsId);
            result = newsService.updateNews(news);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 审批通过资讯
     */
    @PreAuthorize("@ss.hasPermi('business:news:pass')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @DeleteMapping("/pass/{newsIds}")
    public AjaxResult pass(@PathVariable Long[] newsIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        News news = new News();
        news.setState("definite");
        Long approveId = SecurityUtils.getLoginUser().getUser().getUserId();
        news.setApproveid(approveId);
        String approveBy = SecurityUtils.getLoginUser().getUser().getUserName();
        news.setApproveby(approveBy);
        // 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        news.setApprovetime(timeStamp);
        for(Long newsId : newsIds){
            news.setId(newsId);
            result = newsService.updateNews(news);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 反审批退回资讯
     */
    @PreAuthorize("@ss.hasPermi('business:news:back')")
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @DeleteMapping("/back/{newsIds}")
    public AjaxResult back(@PathVariable Long[] newsIds)
    {
        // 更新审核状态为“待审核”的条数计数变量
        int result = 1;
        // 创建实例，将状态设置成待审批
        News news = new News();
        news.setState("approvalback");
        Long approveId = SecurityUtils.getLoginUser().getUser().getUserId();
        news.setApproveid(approveId);
        String approveBy = SecurityUtils.getLoginUser().getUser().getUserName();
        news.setApproveby(approveBy);
        // 创建Date对象，并得到一个Timestamp格式的时间，存入mysql中的时间格式“yyyy/MM/dd HH:mm:ss”
        Date date = new Date();
        Timestamp timeStamp = new Timestamp(date.getTime());
        news.setApprovetime(timeStamp);
        for(Long newsId : newsIds){
            news.setId(newsId);
            result = newsService.updateNews(news);
            if(result <= 0)
            {
                return toAjax(result);
            }
        }
        return toAjax(result);
    }

    /**
     * 删除资讯
     */
    @PreAuthorize("@ss.hasPermi('business:news:remove')")
    @Log(title = "资讯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds)
    {
        return toAjax(newsService.deleteNewsByIds(newsIds));
    }
}
